#include "tic.h"
#include <QGridLayout>
#include <QPushButton>
#include <QDialog>
#include <QLabel>
#include <QComboBox>
#include <QMessageBox>

Tic::Tic(QWidget *parent) : QDialog(parent)
{
    m_Button11 = new QPushButton(" ", this);
    m_Button12 = new QPushButton(" ", this);
    m_Button13 = new QPushButton(" ", this);
    m_Button21 = new QPushButton(" ", this);
    m_Button22 = new QPushButton(" ", this);
    m_Button23 = new QPushButton(" ", this);
    m_Button31 = new QPushButton(" ", this);
    m_Button32 = new QPushButton(" ", this);
    m_Button33 = new QPushButton(" ", this);

    m_comboBox = new QComboBox(this);
    m_comboBox->addItem("x");
    m_comboBox->addItem("o");
    m_comboBox->hide();

    gridlay = new QGridLayout();

    gridlay->addWidget(m_Button11, 1, 1);
    gridlay->addWidget(m_Button12, 1, 2);
    gridlay->addWidget(m_Button13, 1, 3);
    gridlay->addWidget(m_Button21, 2, 1);
    gridlay->addWidget(m_Button22, 2, 2);
    gridlay->addWidget(m_Button23, 2, 3);
    gridlay->addWidget(m_Button31, 3, 1);
    gridlay->addWidget(m_Button32, 3, 2);
    gridlay->addWidget(m_Button33, 3, 3);
    gridlay->addWidget(m_comboBox, 4, 2);

    connect(m_Button11, SIGNAL(clicked()), this, SLOT(changeButton11()));
    connect(m_Button12, SIGNAL(clicked()), this, SLOT(changeButton12()));
    connect(m_Button13, SIGNAL(clicked()), this, SLOT(changeButton13()));
    connect(m_Button21, SIGNAL(clicked()), this, SLOT(changeButton21()));
    connect(m_Button22, SIGNAL(clicked()), this, SLOT(changeButton22()));
    connect(m_Button23, SIGNAL(clicked()), this, SLOT(changeButton23()));
    connect(m_Button31, SIGNAL(clicked()), this, SLOT(changeButton31()));
    connect(m_Button32, SIGNAL(clicked()), this, SLOT(changeButton32()));
    connect(m_Button33, SIGNAL(clicked()), this, SLOT(changeButton33()));

    setLayout(gridlay);
}

void Tic::checkGame()
{
    if (( (m_Button11->text() == m_Button22->text() ) && (m_Button22->text() == m_Button33->text() ) && (m_Button22->text()!= " ") ) ||
        ( (m_Button13->text() == m_Button22->text() ) && (m_Button22->text() == m_Button31->text() ) && (m_Button22->text()!= " ") ) ||
        ( (m_Button11->text() == m_Button12->text() ) && (m_Button12->text() == m_Button13->text() ) && (m_Button12->text()!= " ") ) ||
        ( (m_Button21->text() == m_Button22->text() ) && (m_Button22->text() == m_Button23->text() ) && (m_Button22->text()!= " ") ) ||
        ( (m_Button31->text() == m_Button32->text() ) && (m_Button32->text() == m_Button33->text() ) && (m_Button32->text()!= " ") ) ||
        ( (m_Button11->text() == m_Button21->text() ) && (m_Button21->text() == m_Button31->text() ) && (m_Button21->text()!= " ") ) ||
        ( (m_Button12->text() == m_Button22->text() ) && (m_Button22->text() == m_Button32->text() ) && (m_Button22->text()!= " ") ) ||
        ( (m_Button13->text() == m_Button23->text() ) && (m_Button23->text() == m_Button33->text() ) && (m_Button23->text()!= " ") ))
        QMessageBox::information(this, "info", "Game is over!");
}


void Tic::changeButton11()
{
    if (QString::compare(m_comboBox->currentText(), "x")==0)
        m_Button11= new QPushButton("x", this);
    else
        m_Button11 = new QPushButton("o", this);
    gridlay->addWidget(m_Button11, 1, 1);
    m_comboBox->setCurrentIndex(1 - m_comboBox->currentIndex());
    checkGame();
}

void Tic::changeButton12()
{
    if (QString::compare(m_comboBox->currentText(), "x")==0)
        m_Button12= new QPushButton("x", this);
    else
        m_Button12 = new QPushButton("o", this);
    gridlay->addWidget(m_Button12, 1, 2);
    m_comboBox->setCurrentIndex(1 - m_comboBox->currentIndex());
    checkGame();
}

void Tic::changeButton13()
{
    if (QString::compare(m_comboBox->currentText(), "x")==0)
        m_Button13= new QPushButton("x", this);
    else
        m_Button13 = new QPushButton("o", this);
    gridlay->addWidget(m_Button13, 1, 3);
    m_comboBox->setCurrentIndex(1 - m_comboBox->currentIndex());
    checkGame();
}

void Tic::changeButton21()
{
    if (QString::compare(m_comboBox->currentText(), "x")==0)
        m_Button21= new QPushButton("x", this);
    else
        m_Button21 = new QPushButton("o", this);
    gridlay->addWidget(m_Button21, 2, 1);
   m_comboBox->setCurrentIndex(1 - m_comboBox->currentIndex());
    checkGame();
}

void Tic::changeButton22()
{
    if (QString::compare(m_comboBox->currentText(), "x")==0)
        m_Button22= new QPushButton("x", this);
    else
        m_Button22 = new QPushButton("o", this);
    gridlay->addWidget(m_Button22, 2, 2);
    m_comboBox->setCurrentIndex(1 - m_comboBox->currentIndex());
    checkGame();
}

void Tic::changeButton23()
{
    if (QString::compare(m_comboBox->currentText(), "x")==0)
        m_Button23= new QPushButton("x", this);
    else
        m_Button23 = new QPushButton("o", this);
    gridlay->addWidget(m_Button23, 2, 3);
    m_comboBox->setCurrentIndex(1 - m_comboBox->currentIndex());
    checkGame();
}

void Tic::changeButton31()
{
    if (QString::compare(m_comboBox->currentText(), "x")==0)
        m_Button31= new QPushButton("x", this);
    else
        m_Button31 = new QPushButton("o", this);
    gridlay->addWidget(m_Button31, 3, 1);
    m_comboBox->setCurrentIndex(1 - m_comboBox->currentIndex());
    checkGame();
}

void Tic::changeButton32()
{
    if (QString::compare(m_comboBox->currentText(), "x")==0)
        m_Button32= new QPushButton("x", this);
    else
        m_Button32 = new QPushButton("o", this);
    gridlay->addWidget(m_Button32, 3, 2);
    m_comboBox->setCurrentIndex(1 - m_comboBox->currentIndex());
    checkGame();
}

void Tic::changeButton33()
{
    if (QString::compare(m_comboBox->currentText(), "x")==0)
        m_Button33= new QPushButton("x", this);
    else
        m_Button33 = new QPushButton("o", this);
    gridlay->addWidget(m_Button33, 3, 3);
    m_comboBox->setCurrentIndex(1 - m_comboBox->currentIndex());
    checkGame();
}

